#!/bin/bash
exec_name=EttaM1
cur_user=$(who | awk '{print $1}')

sys() {
# skip disk check
echo 123 | sudo -S sed -i "s/\s[1-9]$/0/g" /etc/fstab

# close daily update
echo 123 | sudo -S systemctl stop apt-daily.timer
echo 123 | sudo -S systemctl stop apt-daily.service
echo 123 | sudo -S systemctl stop apt-daily-upgrade.timer
echo 123 | sudo -S systemctl stop apt-daily-upgrade.service
echo 123 | sudo -S systemctl disable apt-daily.timer
echo 123 | sudo -S systemctl disable apt-daily.service
echo 123 | sudo -S systemctl disable apt-daily-upgrade.timer
echo 123 | sudo -S systemctl disable apt-daily-upgrade.service

# close bug report
echo 123 | sudo -S systemctl stop apport.service
echo 123 | sudo -S systemctl disable apport.service
echo 123 | sudo -S sed -i "s/enabled=1/enabled=0/g" /etc/default/apport
#echo 123 | sudo -S sysctl -p

# hide ubuntu logo
echo 123 | sudo -S sed -i "/^WatermarkHorizontalAlignment=/c WatermarkHorizontalAlignment=1.5" /usr/share/plymouth/themes/default.plymouth
echo 123 | sudo -S sed -i "/^WatermarkVerticalAlignment=/c WatermarkVerticalAlignment=1.96" /usr/share/plymouth/themes/default.plymouth

# optimize boot time
# systemd-analyze blame
echo 123 | sudo -S systemctl disable NetworkManager-wait-online.service
echo 123 | sudo -S systemctl mask plymouth-quit-wait.service
echo 123 | sudo -S systemctl disable snapd.service
}

auth() {
# authorize serial port
echo 123 | sudo -S usermod -aG dialout $cur_user

# authorize usb port
make_udev_rules() {
echo 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="5345", ATTRS{idProduct}=="1235" GOTO="vds_redirect"'
echo 'GOTO="vds_check_end"'
echo 'LABEL="vds_redirect"'
echo 'SUBSYSTEMS=="usbmisc", GROUP="dialout", MODE="0666", SYMLINK+="vds"'
echo 'LABEL="vds_check_end"'
}
make_udev_rules > /tmp/vds.rules
echo 123 | sudo -S mv /tmp/vds.rules /etc/udev/rules.d/vds.rules
rm -f /tmp/vds.rules
}

env() {
enable_desktop_icon() {
local user_desktop_dir=/home/$cur_user/Desktop
local user_desktop_dock_dir=/home/$cur_user/.local/share/applications

[ ! -f $user_desktop_dock_dir/$exec_name.desktop ] && return

rm -f $user_desktop_dir/$exec_name*.desktop >/dev/null 2>&1
cp -f $user_desktop_dock_dir/$exec_name.desktop $user_desktop_dir/

gio set $user_desktop_dir/$exec_name.desktop metadata::trusted true
chmod 755 $user_desktop_dir/$exec_name.desktop
[ -z "$(gio info $user_desktop_dir/$exec_name.desktop | grep "metadata::trusted")" ] && rm -f $user_desktop_dir/$exec_name.desktop
}

enable_core_file() {
local pattern_file=/etc/sysctl.conf
local limits_file=/etc/security/limits.conf

[ -z "$(grep '* soft core unlimited' $limits_file)" ] && { echo 123 | sudo -S bash -c "echo \* soft core unlimited >> $limits_file"; }
[ -z "$(grep '* hard core unlimited' $limits_file)" ] && { echo 123 | sudo -S bash -c "echo \* hard core unlimited >> $limits_file"; }

#if [ -z "$(grep kernel.core_pattern $pattern_file)" ]; then
#  echo 123 | sudo -S bash -c "echo kernel.core_pattern=core >> $pattern_file";
#else
#  echo 123 | sudo -S sed -i "/kernel.core_pattern=/c kernel.core_pattern=core" $pattern_file 
#fi
}

enable_core_file
enable_desktop_icon
}

app() {
[ ! $# -ge 1 ] && return
local install_log_dir=/tmp/$exec_name/log
mkdir -p $install_log_dir >/dev/null 2>&1
echo 123 | sudo dpkg -i $1 >$install_log_dir/$1.log
echo 123 | sudo -S chown $cur_user:$cur_user /home/$cur_user/$exec_name -R
cat $install_log_dir/$1.log
}


###
sys
auth
app $1
env
###

for i in "$@"; do
  if [ "${i}" == "reboot" ]; then
    option="reboot"
    break
  fi
done

case $option in
  "reboot")
    #echo 123 | sudo -S systemctl restart gdm
    reboot
    ;;

  *)
    ;;
esac
